package com.xinya.wanandroidclient.ui.tologin.login

import android.os.Bundle
import android.text.InputType
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.util.Log
import android.view.inputmethod.EditorInfo
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import com.xinya.wanandroidclient.R
import com.xinya.wanandroidclient.base.BaseFragment
import com.xinya.wanandroidclient.common.setNoRepeatClick
import com.xinya.wanandroidclient.databinding.FragmentLoginBinding
import com.xinya.wanandroidclient.utils.KeyBoardUtil
import com.xinya.wanandroidclient.view.NavHostFragment

/**
 *
 * @ClassName:      LoginFragment$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/3/9$ 15:44$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class LoginFragment : BaseFragment() {
    override fun getLayoutId() = R.layout.fragment_login

    companion object {
        private const val TAG = "LoginFragment"
    }

    private val loginLiveData by lazy {
        getFragmentVieModel(LoginViewModel::class.java)
    }

    private lateinit var dataBinding: FragmentLoginBinding

    override fun initDataBinding(binding: ViewDataBinding?) {
        dataBinding = binding as FragmentLoginBinding
    }

    override fun init(savedInstanceState: Bundle?) {
        val loginUsernameView = dataBinding.loginUsername
        val loginPasswordView = dataBinding.loginPassword

        dataBinding.apply {
            btLogin.setNoRepeatClick {
                // 登录
                val username = loginUsernameView.text.toString().trim()
                val password = loginPasswordView.text.toString().trim()

                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(context, "用户名不能为空", Toast.LENGTH_SHORT).show()
                    return@setNoRepeatClick
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(context, "密码不能为空", Toast.LENGTH_SHORT).show()
                    return@setNoRepeatClick
                }
                KeyBoardUtil.closeKeyboard(loginUsernameView, requireContext())
                KeyBoardUtil.closeKeyboard(loginPasswordView, requireContext())
                loginLiveData.login(username, password)

            }
            loginIvUsername.setOnClickListener {
                // 清空userName的输入
                loginUsernameView.text.clear()
            }
            loginIvPassword.setOnClickListener {
                // 更改密码的显示状态
                if (EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD == loginPasswordView.inputType) {
                    // 设置为不可见
                    loginPasswordView.inputType = EditorInfo.TYPE_TEXT_VARIATION_PASSWORD
                    loginPasswordView.transformationMethod = PasswordTransformationMethod.getInstance()
                    loginIvPassword.setImageResource(R.drawable.svg_hide_pass)

                } else {
                    // 设置为可见loginPasswordView
                    loginPasswordView.inputType = EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                    loginPasswordView.transformationMethod = HideReturnsTransformationMethod.getInstance()
                    loginIvPassword.setImageResource(R.drawable.svg_show_pass)

                }
                // 吧光标移动到尾端
                loginPasswordView.setSelection(loginPasswordView.text.toString().length)
            }
        }
    }

    override fun observer() {
        loginLiveData.apply {
            loginLiveData.observe(this@LoginFragment) {
                Toast.makeText(context, "登陆成功", Toast.LENGTH_SHORT).show()
                NavHostFragment.findNavController(this@LoginFragment).navigateUp()
            }

            errorLiveData.observe(this@LoginFragment) {
                when (it.errorCode) {
                    -1001 -> {
                        // TODO: 2021/3/11 进行未登录处理
                    }
                    else -> {
                    }
                }
                Toast.makeText(context, it.errorMsg, Toast.LENGTH_SHORT).show()
            }
        }
    }
}